package com.demo2do.fms.admin.service.authority.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.fms.admin.dao.authority.ResourceDao;
import com.demo2do.fms.admin.dao.authority.RoleDao;
import com.demo2do.fms.admin.dao.authority.UserDao;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.authority.RoleService;
import com.demo2do.fms.entity.authority.Resource;
import com.demo2do.fms.entity.authority.ResourceType;
import com.demo2do.fms.entity.authority.Role;
import com.demo2do.fms.entity.authority.RoleResource;
import com.demo2do.fms.entity.authority.RoleTemplate;

/**
 * 
 * @author Ysy
 *
 */
@Service("roleService")
public class RoleServiceImpl extends DataServiceImpl<Role> implements RoleService {

    @Dao
    private RoleDao roleDao;
    
    @Dao
    private UserDao userDao;
    
    @Dao
    private ResourceDao resourceDao;
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.RoleService#list()
     */
    public Map<String, Role> list() {
        List<Role> roles = listAll(Role.class);
        Map<String, Role> roleMaps = CollectionUtils.constructMapValue(roles, "code");
        return roleMaps;
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.RoleService#list(java.util.List)
     */
    public List<Role> list(List<RoleTemplate> templates) {
        return roleDao.list(templates);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.RoleService#count(com.demo2do.fms.entity.authority.RoleTemplate)
     */
    public int count(RoleTemplate template) {
        return roleDao.count(template);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.RoleService#hasName(java.lang.String)
     */
    public Boolean exsitName(String roleName) {
        return roleDao.load(roleName) == null ? false : true;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.RoleService#create(com.demo2do.fms.entity.authority.Role, java.util.List)
     */
    public void create(Role role, List<String> menuNames) {
        
        // 1. save role
        genericDaoSupport.save(role.initialize());
        
        // 2. save role_resource of the role
        Map<String, Resource> menuResources = this.getMenuResource();
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(menuNames)){
            
            for(String menuName : menuNames) {
                
                Resource resource = menuResources.get(menuName);
                
                RoleResource roleResource = new RoleResource(role, resource);
                genericDaoSupport.save(roleResource);
                
            }
        }
        
        
    }
    
    /**
     * 
     * @return
     */
    private Map<String, Resource> getMenuResource() {
        
        // get resource mapped by name with only menu and submenu type
        
        List<Resource> resources = resourceDao.list(Arrays.asList(ResourceType.MENU, ResourceType.SUBMENU));
        
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(resources)) {
            return CollectionUtils.constructMapValue(resources, "name");
        }
                    
        return null;
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.authority.RoleService#disable(com.demo2do.fms.entity.authority.Role)
     */
    public void disable(Role role) {
        
        if( userDao.count( role.getCode() ) >0 ) {
            throw new BusinessException("删除角色失败，角色已被使用");
        }
        
        // disable role
        genericDaoSupport.update(role.disable());
        
        // delete role
        resourceDao.delete(role.getCode());
        
    }
    
}
